package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: chenheng
 * @Date: 2023/12/18 10:47
 */
@Service
@Slf4j
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        List<TeachplanDto> teachplanDtos = teachplanMapper.selectTreeNodes(courseId);
        return teachplanDtos;
    }
    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto dto) {
        Long id = dto.getId();
        //判断是修改还是插入
        if (id==null){
            //取出同级有多少个，进行排序
            int count = getTeachplanCount(dto);
            Teachplan teachplanNew = new Teachplan();
            BeanUtils.copyProperties(dto,teachplanNew);
            teachplanNew.setOrderby(count+1);
            teachplanMapper.insert(teachplanNew);
        }else {
            //修改
            Teachplan teachplanNew = new Teachplan();
            BeanUtils.copyProperties(dto,teachplanNew);
            teachplanMapper.updateById(teachplanNew);
        }
    }
    @Transactional
    @Override
    public void deleteTeachplan(Long coursePlanId) {
        //判断是大章节还是小节
        Teachplan teachplan = teachplanMapper.selectById(coursePlanId);
        if (teachplan == null){
            XueChengPlusException.cast("删除失败，此章节已删除");
        }
        //课程层级grade
        int grade = teachplan.getGrade();
        if (grade == 1){
            //大章节 大章节下边没有小章节时方可删除
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teachplan::getParentid,teachplan.getId());
            List<Teachplan> teachplans = teachplanMapper.selectList(wrapper);
            if (teachplans.size()==0){
                //删除
                teachplanMapper.deleteById(coursePlanId);
            }else {
                XueChengPlusException.cast("删除失败，大章节下边有小章节");
            }
        }else {
            //小节 将teachplan_media表关联的信息也删除
            teachplanMapper.deleteById(coursePlanId);
            LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TeachplanMedia::getTeachplanId,coursePlanId);
            teachplanMediaMapper.delete(wrapper);
        }
    }

    @Transactional
    @Override
    public void movedownTeachplan(Long coursePlanId) {
        //找出当前对象
        Teachplan teachplan = teachplanMapper.selectById(coursePlanId);
        Integer currentOrderby = teachplan.getOrderby();
        Integer grade = teachplan.getGrade();
        Long courseId = teachplan.getCourseId();
        Long parentid = teachplan.getParentid();
        //找到待交换的对象
        Teachplan teachplanChange = teachplanMapper.movedownTeachplan(grade, courseId, currentOrderby, parentid);
        if (teachplanChange == null){
            XueChengPlusException.cast("当前为最后一个章节，不可移动");
        }
        //交换orderby
        Integer temp = teachplan.getOrderby();
        teachplan.setOrderby(teachplanChange.getOrderby());
        teachplanChange.setOrderby(temp);
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplanChange);
    }

    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //课程计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            XueChengPlusException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengPlusException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //课程id
        Long courseId = teachplan.getCourseId();
        //先删除原来该教学计划绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId,teachplanId));
        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }

    @Override
    public void deletAssociationMedia(Long teachPlanId, String mediaId) {
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachPlanId);
        queryWrapper.eq(TeachplanMedia::getMediaId,mediaId);
        //先删除原来该教学计划绑定的媒资
        int delete = teachplanMediaMapper.delete(queryWrapper);
        if (delete<=0){
            XueChengPlusException.cast("不存在绑定关系，删除失败");
        }
    }

    private int getTeachplanCount(SaveTeachplanDto dto){
        Long parentid = dto.getParentid();
        Integer grade = dto.getGrade();
        Long courseId = dto.getCourseId();
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getGrade,grade).eq(Teachplan::getCourseId,courseId);
        wrapper.eq(Teachplan::getParentid,parentid);
        Integer i = teachplanMapper.selectCount(wrapper);
        return i;
    }
}
